package sdk3.components.remoteService
{
	import sdk3.c_OBJECT;
	import sdk3.components.remoteService.remoteService_utility;

	/**
	 Software: connectionParameters.as												
	 Version:  1.0
	 Date:     2010_12_21_TIME_08:44:18
	 Author:   
	 
	 PURPOSE:
	 defines a database-connection-parameter-object (only needed if the server has no config.xml or config.ini file from where to read mysql-user/password/port etc.
	 
	 Usage:
	 
	 FEATURES:
	 
	 TODO: XXX:
	 CHANGELOG:
	 there is a endpoint_xml.config on the server where all the connection parameters for the database connection are stored and read from
	 whenever there comes a request from the client.
	 * 
	 * current format is like this: (will possibly change and this docu will not be in sync with the change)

	<?xml version="1.0" encoding="utf-8"?>
	<config>
	   <server host="192.168.0.65" url="http://localhost:8080/flexBackend/endpoint_xml"/>
	 </config>

 *  * 	 unless one wants to query multiple data sources from different servers...
	 all that the client needs to know is
	 * 1. how to reach the server... (there can be a config.xml in the same dir as the TOGETHER.swf)
	 * 2. and the server will know how to read the database. (from endpoint_xml.config)
	 (give that the user has access rights to the data)

 * the default format looks like this: (it might have changed and this info might not be updated)
	endpoint_xml.config
	 
	######## MYSQL CONFIG ########
	host=localhost
	DatabaseName=
	type=mysql
	port=3306
	user=
	password=
	usernameColumn=username
	passwordColumn=password
	authTable=users
	 
	 USAGE EXAMPLE:
	 **/
	public class connectionParameters
	{
		/** LICENSE:
		 * 		 * LICENSE:  GNU General Public License (http://www.gnu.org/licenses/gpl.html) 	 *  	 * English: 		This program is free software; 		you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; 		either version 3 of the License, or (at your option) any later version.   		This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 		without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 		See the GNU General Public License for more details. 		You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.   		 * Excerpt English: 		 * Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), 		 * that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things   		Auf Deutsch: 		Dieses Programm ist freie Software. Sie können es unter den Bedingungen der GNU General Public License, wie von der Free Software Foundation veröffentlicht, 		weitergeben und/oder modifizieren, entweder gemäß Version 3 der Lizenz oder (nach Ihrer Option) jeder späteren Version. 		Die Veröffentlichung dieses Programms erfolgt in der Hoffnung, daß es Ihnen von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE, 		sogar ohne die implizite Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK. 		Details finden Sie in der GNU General Public License. 		Sie sollten ein Exemplar der GNU General Public License zusammen mit diesem Programm erhalten haben. 		Falls nicht, siehe <http://www.gnu.org/licenses/>.   		 * Auszug Deutsch: 		 * Wenn wir von freier Software sprechen, so beziehen wir uns auf Freiheit, nicht auf den Preis. 		 * Unsere Allgemeinen Öffentlichen Lizenzen sind darauf angelegt, sicherzustellen, daß Sie die Freiheit haben, 		 * Kopien freier Software zu verbreiten (und dafür etwas zu berechnen, wenn Sie möchten), die Möglichkeit, 		 * daß Sie die Software als Quelltext erhalten oder den Quelltext auf Wunsch bekommen, daß Sie die Software 		 * ändern oder Teile davon in neuen freien Programmen verwenden dürfen und daß Sie wissen, daß Sie dies alles tun dürfen.   GNU General Public License (http://www.gnu.org/licenses/gpl.html)
		 * 
		 * English:
		 This program is free software;
		 you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation;
		 either version 3 of the License, or (at your option) any later version.
		 
		 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
		 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
		 See the GNU General Public License for more details.
		 You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
		 
		 * Excerpt English:
		 * Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish),
		 * that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things
		 
		 Auf Deutsch:
		 Dieses Programm ist freie Software. Sie können es unter den Bedingungen der GNU General Public License, wie von der Free Software Foundation veröffentlicht,
		 weitergeben und/oder modifizieren, entweder gemäß Version 3 der Lizenz oder (nach Ihrer Option) jeder späteren Version.
		 Die Veröffentlichung dieses Programms erfolgt in der Hoffnung, daß es Ihnen von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE,
		 sogar ohne die implizite Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK.
		 Details finden Sie in der GNU General Public License.
		 Sie sollten ein Exemplar der GNU General Public License zusammen mit diesem Programm erhalten haben.
		 Falls nicht, siehe <http://www.gnu.org/licenses/>.
		 
		 * Auszug Deutsch:
		 * Wenn wir von freier Software sprechen, so beziehen wir uns auf Freiheit, nicht auf den Preis.
		 * Unsere Allgemeinen Öffentlichen Lizenzen sind darauf angelegt, sicherzustellen, daß Sie die Freiheit haben,
		 * Kopien freier Software zu verbreiten (und dafür etwas zu berechnen, wenn Sie möchten), die Möglichkeit,
		 * daß Sie die Software als Quelltext erhalten oder den Quelltext auf Wunsch bekommen, daß Sie die Software
		 * ändern oder Teile davon in neuen freien Programmen verwenden dürfen und daß Sie wissen, daß Sie dies alles tun dürfen.
		**/

		/** defines the very basic database connection parameters:
		 * defaults are set for mysql usage.
		**/
		private var c_OBJECT_inst:c_OBJECT = new c_OBJECT();
		
		public var host:String = 'localhost';
		// * 1. what computer (host)
		public var DatabaseName:String = '';
		// * 2. what kind of database is running (type, possible values:'mysql') 
		public var type:String = 'mysql';
		// * 3. what port is the database running on (port)
		public var port:String = '3306';
		// * 4. what username + password should be used to connect?
		public var user:String = 'root';
		public var TableName:String = '';
		public var password:String = '';
		/** 5. if there is a "gateway" endpoint to send requests to, specify the url (url, e.g. http://localhost/index.php or http://localhost/buha/bin/amfphp/gateway.php')
			 * 
			 * there is no table or database defined, because to keep things flexible, database+table needs to be given at every call made.
			 * **/
		public var url:String = 'http://localhost/index.php';
		/** 6. packet size, because php has timeout limits an packetsize needs to be configured, this is the amount of database entries that will be transfered (to and from the server) during one request (packet_size, default 300 entries)
		* this is especially important when dealing with large sets of data.
		**/
		public var packet_size:int = 300;
		// * 7. class_name:'xdbtools', only importanf for amfphp and blazeds kind of gateways.
		public var class_name:String = 'xdbtools';
		// * 8. dataEncoding means what "protocoll"/transfer method should be used (valid values:'xml' (default) 'amf' (not implemented yet))
		public var dataEncoding:String = 'xml';
		// if the aciton is a read action, in what sorting the results should be returned from backend
		public var SortMode:String = 'ASC';
		public var SortField:String = '';

		/** is variable is used to check if the connection parameters are all valid **/
		public var VALID:Object = {valid:false,reason:""}; 
		public function connectionParameters(host:String = 'localhost',port:String = '3306',DatabaseName:String = '',TableName:String = '',type:String = 'mysql',user:String = 'root',password:String = '',url:String = 'http://localhost/index.php',packet_size:int = 300,class_name = 'xdbtools',dataEncoding='amf',SortMode:String = 'ASC',SortField = '')
		{
			var RSU:remoteService_utility = new remoteService_utility();
			DatabaseName = RSU.mysql_real_escape_string(DatabaseName);
			TableName = RSU.mysql_real_escape_string(TableName);
			user = RSU.mysql_real_escape_string(user);
			password = RSU.mysql_real_escape_string(password);

			VALID['valid'] = true;

			// check if any parameter is wrong
			if(c_OBJECT_inst.empty(DatabaseName))
			{
				VALID['valid'] = false;
				VALID['reason'] = "DatabaseName should not be empty";
				throw new Error("connection_parametersas: connection parameters invalid, reason: "+VALID['reason']);
			}
			else
			{
				this.DatabaseName = DatabaseName;			
			}
			
			if(c_OBJECT_inst.empty(TableName))
			{
				VALID['valid'] = false;
				VALID['reason'] = "database-TableName should not be empty";
				throw new Error("connection_parametersas: connection parameters invalid, reason: "+VALID['reason']);
			}
			else
			{
				this.TableName = TableName;
			}
			if(c_OBJECT_inst.empty(type))
			{
				VALID['valid'] = false;
				VALID['reason'] = "database-type should not be empty";
				throw new Error("connection_parametersas: connection parameters invalid, reason: "+VALID['reason']);
			}
			else
			{
				this.type = type;
			}

			if(c_OBJECT_inst.empty(port))
			{
				VALID['valid'] = false;
				VALID['reason'] = "port should not be empty";
				throw new Error("connection_parametersas: connection parameters invalid, reason: "+VALID['reason']);
			}
			else
			{
				this.port=port;
			}

			if(c_OBJECT_inst.empty(user))
			{
				VALID['valid'] = false;
				VALID['reason'] = "user should not be empty";
				throw new Error("connection_parametersas: connection parameters invalid, reason: "+VALID['reason']);
			}
			else
			{
				this.user=user;
			}

			if(c_OBJECT_inst.empty(password))
			{
				VALID['valid'] = true;
				VALID['reason'] = "password is empty, this is valid but could be a security risk!";
			}
			else
			{
				this.password=password;
			}

			if(c_OBJECT_inst.empty(host))
			{
				VALID['valid'] = false;
				VALID['reason'] = "host should not be empty";
				throw new Error("connection_parametersas: connection parameters invalid, reason: "+VALID['reason']);
			}
			else
			{
				this.host=host;
			}

			if(c_OBJECT_inst.empty(dataEncoding))
			{
				VALID['valid'] = false;
				VALID['reason'] = "dataEncoding should not be empty";
				throw new Error("connection_parametersas: connection parameters invalid, reason: "+VALID['reason']);
			}
			else
			{
				this.dataEncoding=dataEncoding;
			}

			if(c_OBJECT_inst.empty(class_name))
			{
				VALID['valid'] = false;
				VALID['reason'] = "class_name should not be empty";
				throw new Error("connection_parametersas: connection parameters invalid, reason: "+VALID['reason']);
			}
			else
			{
				this.class_name=class_name;
			}

			if(c_OBJECT_inst.empty(packet_size))
			{
				VALID['valid'] = false;
				VALID['reason'] = "packet_size should not be empty";
				throw new Error("connection_parametersas: connection parameters invalid, reason: "+VALID['reason']);
			}
			else
			{
				this.packet_size=packet_size;
			}
			
			if(c_OBJECT_inst.empty(url))
			{
				VALID['valid'] = false;
				VALID['reason'] = "url should not be empty";
				throw new Error("connection_parametersas: connection parameters invalid, reason: "+VALID['reason']);
			}
			else
			{
				this.url=url;
			}
			
			if(c_OBJECT_inst.empty(SortMode))
			{
				// VALID['valid'] = false;
				// VALID['reason'] = "url should not be empty";
				// throw new Error("connection_parametersas: connection parameters invalid, reason: "+VALID['reason']);
			}
			else
			{
				this.SortMode=SortMode;
			}

			if(c_OBJECT_inst.empty(SortField))
			{
				// VALID['valid'] = false;
				// VALID['reason'] = "url should not be empty";
				// throw new Error("connection_parametersas: connection parameters invalid, reason: "+VALID['reason']);
			}
			else
			{
				this.SortMode=SortField;
			}
		}

		/** did not find any way to iterate over variables of this class so one did it "manually" **/
		public function endpoint_to_request(request:Object):Object
		{
			request['DatabaseName'] = this.DatabaseName;
			request['host'] = this.host;
			request['port'] = this.port;
			request['type'] = this.type;
			request['TableName'] = this.TableName;
			request['user'] = this.user;
			request['password'] = this.password;
			request['url'] = this.url;
			request['packet_size'] = this.packet_size;
			request['class_name'] = this.class_name;
			request['dataEncoding'] = this.dataEncoding;
			request['SortMode'] = this.SortMode;
			request['SortField'] = this.SortField;
			
			return request;
		}
		
	}
}